<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    ul,
    ol,
    li {
      list-style: none;
    }
  </style>
</head>

<body>

  <div id="app">

    <input type="text" v-model="username">
    <div>
      <input type="radio" id="sex1" value="男" v-model="sex">
      <label for="sex1">男</label>
      <br>
      <input type="radio" id="sex2" value="女" v-model="sex">
      <label for="sex2">女</label>
      <br>
    </div>
    <div>
      问候语：{{sayHello}}
    </div>
  </div>

  <script src="./js/vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        sex: '男',
        username: '',
        sayHello: ''
      },
      watch: {
        // watch里面的函数，不能时箭头函数！
        // 自动监听同名的data里面的属性变量值的变化。值改变时，就会执行这个函数。
        // 所以，watch里面的函数名，必须和data里面的被监听的属性名同名。
        sex: function () {
          let s = this.sex === '男' ? '先生' : '女士'
          this.sayHello = this.username.trim() ? this.username + " " + s + ", 您好！" : ''
        },
        username: function () {
          let s = this.sex === '男' ? '先生' : '女士'
          this.sayHello = this.username.trim() ? this.username + " " + s + ", 您好！" : ''
        }
      }

    });


  </script>
</body>

</html>